<?php

/*
 * Nibbleblog -
 * http://www.nibbleblog.com
 * Author Diego Najar

 * All Nibbleblog code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 */

class Filesystem {

	public static function get_images($regex)
	{

		return self::ls(PATH_UPLOAD, $regex, '*', false, false, false);

	}
	public static function tmpGlob($path,$file_expression,$ext) {

		//clean the tme dir
		Filesystem::delFile(SAE_TMP_PATH);
		if(strstr($path, 'saekv'))
		{
			$per = substr($path,8);
			$kv = new SaeKV();
			$ret = $kv->init();
			$ret = $kv->pkrget($per, 100);
			while (true) {
				foreach($ret as $k=>$v)
				{
					if(is_file($path.basename($k)))
					{
						copy($path.basename($k),SAE_TMP_PATH.basename($k));
					}

				}
				end($ret);
				$start_key = key($ret);
				$i = count($ret);
				if ($i < 100) break;
				$ret = $kv->pkrget($per, 100, $start_key);
			}
			$fileTmp =  glob(SAE_TMP_PATH . $file_expression . '.' . $ext);
		}
		else if(strstr($path, 'saestor') )
		{
			$stor =  new SaeStorage(SAE_ACCESSKEY,SAE_SECRETKEY);
			$ret = $stor->getListByPath ("blog", "content/public/upload",1000) ;
			$ret =($ret['files']);
			$fileTmp =array();
			foreach(@$ret as $file) {
				copy( 'saestor://blog/'.$file['fullName'],SAE_TMP_PATH.basename($file['fullName']));
				$fileTmp [] = $stor->getUrl("blog",$file['fullName']);
			}
			 $fileTmp  =  glob(SAE_TMP_PATH . $file_expression . '.' . $ext);
			
		}
			
			
			 
		else
		{
			$fileTmp = glob($path . $file_expression . '.' . $ext);
		}

		
		return $fileTmp;
	}

	public static function delFile($dirName){

		if(file_exists($dirName) && $handle=opendir($dirName)){

			while(false!==($item = readdir($handle))){

				if($item!= "." && $item != ".."){

					if(file_exists($dirName.'/'.$item) && is_dir($dirName.'/'.$item)){

						Filesystem::delFile($dirName.'/'.$item);

					}else{

						if(unlink($dirName.'/'.$item)){

							return true;

						}
					}
				}
			}
			closedir( $handle);
		}
	}

	// Devuelve un arreglo con el listado de archivos
	// $path con una barra al final, ej: /home/
	// $file_expression : *.0.*.*.*.*.*.*.*.*
	// $ext : xml
	// $flag_dir : si quiero listar directorios
	// $sort_asc_numeric : ordeno ascedente numerico
	// $sort_desc_numeric : ordeno descendente numerico
	public static function ls($path, $file_expression = NULL, $ext, $flag_dir = false, $sort_asc_numeric = false, $sort_desc_numeric = true)
	{
		if($flag_dir)
		{
			$files = glob($path . $file_expression, GLOB_ONLYDIR);
			//$files = Filesystem::tmpGlob($path,$file_expression, GLOB_ONLYDIR);
		}
		else
		{
			//$files = glob($path . $file_expression . '.' . $ext);
			$files = Filesystem::tmpGlob($path,$file_expression,$ext);
		}

		if( ($files==false) || (empty($files)) )
		{
			$files = array();
		}

		foreach($files as $key=>$file)
		{
			$files[$key] = basename($file);
		}

		// Sort
		if($sort_asc_numeric)
		{
			sort($files, SORT_NUMERIC);
		}
		elseif($sort_desc_numeric)
		{
			rsort($files, SORT_NUMERIC);
		}

		return $files;
	}
}

?>